package com.inei.cenacom.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import com.inei.cenacom.R;
import com.inei.cenacom.controller.Cap100Controller;
import com.inei.cenacom.controller.Cap200Controller;
import com.inei.cenacom.controller.Cap300Controller;
import com.inei.cenacom.controller.Cap400Controller;
import com.inei.cenacom.controller.CaratulaController;
import com.inei.cenacom.controller.EntrevistaController;
import com.inei.cenacom.controller.MarcoController;
import com.inei.cenacom.model.entity.Cap100;
import com.inei.cenacom.model.entity.Cap200;
import com.inei.cenacom.model.entity.Cap300;
import com.inei.cenacom.model.entity.Cap400;
import com.inei.cenacom.model.entity.Caratula;
import com.inei.cenacom.model.entity.Entrevista;
import com.inei.cenacom.model.entity.Login;
import com.inei.cenacom.model.entity.Marco;
import com.inei.cenacom.tools.Enviar;
import com.inei.cenacom.tools.Exportar;
import com.inei.cenacom.tools.Mensaje;

public class Marco_Activity extends Activity {

	ListView lvMarco;
	MarcoController con;
	CaratulaController cCon;
	List<Marco> lista;
	List<HashMap<String, String>> lMap;
	Mensaje m;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_marco);
		iniciarComponentes();
		cargarMarco();
	}

	public void iniciarComponentes() {
		con = new MarcoController(this);
		cCon = new CaratulaController(this);
		m = new Mensaje(this);
		lvMarco = (ListView) findViewById(R.id.lvMarco);
		lvMarco.setDividerHeight(0);
		registerForContextMenu(lvMarco);
	}

	public void cargarMarco() {
		Login login = new Login();
		login.setDEP_ASIG(getIntent().getExtras().getString("DEP_ASIG"));
		login.setUSUARIO(getIntent().getExtras().getString("USUARIO"));
		lista = con.leer(login);
		if (lista != null) {
			convertir(lista);
			SimpleAdapter adapter = new SimpleAdapter(this, lMap,
					R.layout.marco_item, new String[] { "NRO", "RUTA", "2013",
							"2011", "EXPORTACION", "GPS" }, new int[] {
							R.id.tvNRO, R.id.tvRuta, R.id.tv2013, R.id.tv2011,
							R.id.tvExportacion, R.id.tvGPS });
			lvMarco.setAdapter(adapter);
		} else {
			lvMarco.setAdapter(null);
			lMap = null;
		}

	}

	private void convertir(List<Marco> lista2) {
		if (lMap == null) {
			lMap = new ArrayList<HashMap<String, String>>();
		} else {
			lMap.clear();
		}

		for (int i = 0; i < lista.size(); i++) {
			Marco o = lista.get(i);
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("NRO", "" + (i + 1));
			map.put("RUTA", o.getID_N());
			Caratula c = new Caratula();
			c.setID_N(o.getID_N());
			c.setANIO(2013);
			c = cCon.buscar(c);
			if (c != null) {
				if (c.getIVRESFIN_02() == 1 || c.getIVRESFIN_02() == 7
						|| c.getIVRESFIN_02() == 8 || c.getIVRESFIN_02() == 9) {
					map.put("2013", "V");
				} else {
					map.put("2013", "X");
				}
			} else {
				map.put("2013", "-");
			}

			c = new Caratula();
			c.setID_N(o.getID_N());
			c.setANIO(2011);
			c = cCon.buscar(c);
			if (c != null) {
				if (c.getIVRESFIN_02() == 1 || c.getIVRESFIN_02() == 7
						|| c.getIVRESFIN_02() == 8 || c.getIVRESFIN_02() == 9) {
					map.put("2011", "V");
				} else {
					map.put("2011", "X");
				}
			} else {
				map.put("2011", "-");
			}

			map.put("EXPORTACION", "-");

			EntrevistaController eCon = new EntrevistaController(this);
			List<Entrevista> lEnt = eCon.EntrevistaObtenerTodo(o.getID_N(),
					2013);
			if (lEnt != null) {
				map.put("GPS", lEnt.get(0).getGPSLATITUD_I() + "   "
						+ lEnt.get(0).getGPSLONGITUD_I());
			} else {
				map.put("GPS", "-");
			}
			lMap.add(map);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_marco, menu);
		menu.setHeaderTitle("Menu");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		String ID_N = "";
		int ANIO = 0;
		Caratula caratula;
		CaratulaController carCon;
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.mnEditar2011:
			Intent irCaratula2011 = new Intent(this, Caratula_Activity.class);
			irCaratula2011.putExtras(getIntent().getExtras());
			irCaratula2011.putExtra("ID_N", lista.get(info.position).getID_N());
			irCaratula2011.putExtra("ANIO", 2011);
			irCaratula2011.putExtra("CCDD", lista.get(info.position).getCCDD());
			irCaratula2011.putExtra("CCPP", lista.get(info.position).getCCPP());
			irCaratula2011.putExtra("CCDI", lista.get(info.position).getCCDI());
			irCaratula2011.putExtra("ZONA", lista.get(info.position).getZONA());
			irCaratula2011.putExtra("ZONALF", lista.get(info.position)
					.getZONALF());
			irCaratula2011.putExtra("MANZANA", lista.get(info.position)
					.getMANZANA());
			irCaratula2011.putExtra("MZNALF", lista.get(info.position)
					.getMZNALF());
			irCaratula2011.putExtra("AER_INI", lista.get(info.position)
					.getAER_INI());
			irCaratula2011.putExtra("AER_FIN", lista.get(info.position)
					.getAER_FIN());
			irCaratula2011.putExtra("RUTA", lista.get(info.position)
					.getREGISTRADOR());
			startActivity(irCaratula2011);
			finish();
			break;
		case R.id.mnEditar2013:
			Intent irCaratula2013 = new Intent(this, Caratula_Activity.class);
			irCaratula2013.putExtras(getIntent().getExtras());
			irCaratula2013.putExtra("ID_N", lista.get(info.position).getID_N());
			irCaratula2013.putExtra("ANIO", 2013);
			irCaratula2013.putExtra("CCDD", lista.get(info.position).getCCDD());
			irCaratula2013.putExtra("CCPP", lista.get(info.position).getCCPP());
			irCaratula2013.putExtra("CCDI", lista.get(info.position).getCCDI());
			irCaratula2013.putExtra("ZONA", lista.get(info.position).getZONA());
			irCaratula2013.putExtra("ZONALF", lista.get(info.position)
					.getZONALF());
			irCaratula2013.putExtra("MANZANA", lista.get(info.position)
					.getMANZANA());
			irCaratula2013.putExtra("MZNALF", lista.get(info.position)
					.getMZNALF());
			irCaratula2013.putExtra("AER_INI", lista.get(info.position)
					.getAER_INI());
			irCaratula2013.putExtra("AER_FIN", lista.get(info.position)
					.getAER_FIN());
			irCaratula2013.putExtra("RUTA", lista.get(info.position)
					.getREGISTRADOR());
			startActivity(irCaratula2013);
			finish();
			break;
		case R.id.mnExportar2011:
			ID_N = lista.get(info.position).getID_N();
			ANIO = 2011;
			carCon = new CaratulaController(getApplicationContext());
			caratula = new Caratula();
			caratula.setID_N(ID_N);
			caratula.setANIO(ANIO);
			caratula = carCon.buscar(caratula);
			if (caratula == null) {
				m.Error("NO HAY REGISTROS");
			} else {
				EntrevistaController conEnt = new EntrevistaController(
						getApplicationContext());
				List<Entrevista> lEnt = conEnt
						.EntrevistaObtenerTodo(ID_N, ANIO);
				if (lEnt == null) {
					m.Error("NO HAY VISITAS");
				} else {
					Cap100Controller con100 = new Cap100Controller(
							getApplicationContext());
					Cap100 cap100 = new Cap100();
					cap100.setID_N(ID_N);
					cap100.setANIO(ANIO);
					cap100 = con100.buscar(cap100);

					Cap200Controller con200 = new Cap200Controller(
							getApplicationContext());
					List<Cap200> lCap200 = new ArrayList<Cap200>();
					lCap200 = con200.leer(ID_N, ANIO);

					Cap300Controller con300 = new Cap300Controller(
							getApplicationContext());
					List<Cap300> lCap300 = new ArrayList<Cap300>();
					lCap300 = con300.leer(ID_N, ANIO);

					Cap400Controller con400 = new Cap400Controller(
							getApplicationContext());
					List<Cap400> lCap400 = new ArrayList<Cap400>();
					lCap400 = con400.Cap400ObtenerTodo(ID_N, ANIO);

					Exportar exportar = new Exportar(getApplicationContext());
					if (exportar.xml(caratula, lEnt, cap100, lCap200, lCap300,
							lCap400)) {
						m.Exito("Datos exportados");
					}else{
						m.Exito("No se pudieron exportar los datos");
					}

				}
			}
			break;
		case R.id.mnExportar2013:
			ID_N = lista.get(info.position).getID_N();
			ANIO = 2013;
			carCon = new CaratulaController(getApplicationContext());
			caratula = new Caratula();
			caratula.setID_N(ID_N);
			caratula.setANIO(ANIO);
			caratula = carCon.buscar(caratula);
			if (caratula == null) {
				m.Error("NO HAY REGISTROS");
			} else {
				EntrevistaController conEnt = new EntrevistaController(
						getApplicationContext());
				List<Entrevista> lEnt = conEnt
						.EntrevistaObtenerTodo(ID_N, ANIO);
				if (lEnt == null) {
					m.Error("NO HAY VISITAS");
				} else {
					Cap100Controller con100 = new Cap100Controller(
							getApplicationContext());
					Cap100 cap100 = new Cap100();
					cap100.setID_N(ID_N);
					cap100.setANIO(ANIO);
					cap100 = con100.buscar(cap100);

					Cap200Controller con200 = new Cap200Controller(
							getApplicationContext());
					List<Cap200> lCap200 = new ArrayList<Cap200>();
					lCap200 = con200.leer(ID_N, ANIO);

					Cap300Controller con300 = new Cap300Controller(
							getApplicationContext());
					List<Cap300> lCap300 = new ArrayList<Cap300>();
					lCap300 = con300.leer(ID_N, ANIO);

					Cap400Controller con400 = new Cap400Controller(
							getApplicationContext());
					List<Cap400> lCap400 = new ArrayList<Cap400>();
					lCap400 = con400.Cap400ObtenerTodo(ID_N, ANIO);

					Exportar exportar = new Exportar(getApplicationContext());
					if (exportar.xml(caratula, lEnt, cap100, lCap200, lCap300,
							lCap400)) {
						m.Exito("Datos exportados");
					}else{
						m.Exito("No se pudieron exportar los datos");
					}

				}
			}
			break;
		case R.id.mnEnviar2011:
			ID_N = lista.get(info.position).getID_N();
			ANIO = 2011;
			CaratulaController cCon = new CaratulaController(
					getApplicationContext());
			Caratula c = new Caratula();
			c.setID_N(ID_N);
			c.setANIO(ANIO);
			c = cCon.buscar(c);

			EntrevistaController eCon = new EntrevistaController(this);
			List<Entrevista> lEnt = eCon.EntrevistaObtenerTodo(ID_N, ANIO);
			Cap100Controller con100 = new Cap100Controller(this);
			Cap100 c100 = new Cap100();
			c100.setID_N(ID_N);
			c100.setANIO(ANIO);
			c100 = con100.buscar(c100);
			if (c == null || lEnt == null) {
				m.Error("Aun no ha ingresado datos para esta dependencia");
				break;
			}

			Cap200Controller con200 = new Cap200Controller(this);
			Cap300Controller con300 = new Cap300Controller(this);
			Cap400Controller con400 = new Cap400Controller(this);

			Enviar enviar = new Enviar(this, c, lEnt, c100, con200.leer(ID_N,
					ANIO), con300.leer(ID_N, ANIO), con400.Cap400ObtenerTodo(
					ID_N, ANIO));
			enviar.execute();
			break;
		case R.id.mnEnviar2013:
			ID_N = lista.get(info.position).getID_N();
			ANIO = 2013;
			CaratulaController cCon1 = new CaratulaController(
					getApplicationContext());
			Caratula c1 = new Caratula();
			c1.setID_N(ID_N);
			c1.setANIO(ANIO);
			c1 = cCon1.buscar(c1);

			EntrevistaController eCon1 = new EntrevistaController(this);
			List<Entrevista> lEnt1 = eCon1.EntrevistaObtenerTodo(ID_N, ANIO);
			Cap100Controller cont100 = new Cap100Controller(this);
			Cap100 cap100 = new Cap100();
			cap100.setID_N(ID_N);
			cap100.setANIO(ANIO);
			cap100 = cont100.buscar(cap100);
			if (c1 == null || lEnt1 == null) {
				m.Error("Aun no ha ingresado datos para esta dependencia");
				break;
			}

			Cap200Controller cont200 = new Cap200Controller(this);
			Cap300Controller cont300 = new Cap300Controller(this);
			Cap400Controller cont400 = new Cap400Controller(this);

			Enviar enviar1 = new Enviar(this, c1, lEnt1, cap100, cont200.leer(
					ID_N, ANIO), cont300.leer(ID_N, ANIO),
					cont400.Cap400ObtenerTodo(ID_N, ANIO));
			enviar1.execute();
			break;
		case R.id.mnCobertura2011:
			ID_N = lista.get(info.position).getID_N();
			ANIO = 2011;
			Bundle b2011 = getIntent().getExtras();
			b2011.putString("ID_N",ID_N);
			b2011.putInt("ANIO",ANIO);
			Intent irCobertura2011 = new Intent(this,Cobertura_Activity.class);
			irCobertura2011.putExtras(b2011);
			startActivity(irCobertura2011);
			
			break;
		case R.id.mnCobertura2013:
			ID_N = lista.get(info.position).getID_N();
			ANIO = 2013;
			Bundle b2013 = getIntent().getExtras();
			b2013.putString("ID_N",ID_N);
			b2013.putInt("ANIO",ANIO);
			Intent irCobertura2013 = new Intent(this,Cobertura_Activity.class);
			irCobertura2013.putExtras(b2013);
			startActivity(irCobertura2013);
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

}
